<?php

namespace app\service;

use app\common\BaseService;
use app\model\Project as ProjectModel;
use think\facade\Db;

class ProjectService extends BaseService
{
  public static function getInstance(): ProjectService
  {
    return parent::getInstance();
  }

  public static function getModel(): ProjectModel
  {
    return ProjectModel::getInstance();
  }

  /*
   * 根据condition获取项目
   */
  public static function getProjectByCondition ($conditions = [], $order = ['id' => 'asc']) {
    $query = self::getModel();
    foreach ($conditions as $item) {
      list($field, $value, $op) = $item;
      if (!$op) {
        $op = '=';
      }
      $query = $query->where($field, $op, $value);
    }
    $query = $query->order($order);
    return $query->select()->toArray();
  }

  /*
   * 新增项目
   */
  public static function createProject($data) {
    // 判断是否已有同名项目
    $have = self::getModel()
      ->where('project_name', '=', trim($data['project_name']))
      ->where('department_id', '=', $data['department_id'])
      ->select()
      ->toArray();
    if ($have) {
      return ['status' => false, 'msg' => '该项目已存在'];
    }
    # 判断是否含有下划线
    $check = preg_match_all('/.*[-,-].*/', self::getInstance()->sbc2Dbc($data['project_name']));
    if ($check) {
      return ['status' => false, 'msg' => '项目名不能含有下划线'];
    }
    $data['status'] = 1;
    $data['insert_time'] = date('Y-m-d H:i:s');
    $data['last_update_time'] = date('Y-m-d H:i:s');
    $task = [
      'project_name' => trim($data['project_name']),
      'project_user_ids' => $data['project_user_ids'],
      'department_id' => $data['department_id'],
      'desc' => $data['desc'],
      'status' => 1,
      'insert_time' => date('Y-m-d H:i:s'),
      'last_update_time' => date('Y-m-d H:i:s')
    ];
    // 新增task表
    $res = self::getModel()->save($task);
    if ($res === false) {
      return ['status' => false, 'msg' => '新增失败'];
    }
    return ['status' => true, 'msg' => 'ok'];
  }

  /*
   * 编辑任务
   */
  public static function updateProject($id, $data) {
    $data['last_update_time'] = date('Y-m-d H:i:s');
    # 判断是否含有下划线
    $check = preg_match_all('/.*[-,-].*/',$data['project_name']);
    if ($check) {
      return ['status' => false, 'msg' => '项目名不能含有下划线'];
    }
    // 判断是否已有同名项目
    $have = self::getModel()
      ->where('project_name', '=', trim($data['project_name']))
      ->where('department_id', '=', $data['department_id'])
      ->where('id', '<>', $id)
      ->select()
      ->toArray();
    if ($have) {
      return ['status' => false, 'msg' => '该项目已存在'];
    }
    $update = [
      'project_name' => $data['project_name'],
      'project_user_ids' => $data['project_user_ids'],
      'department_id' => $data['department_id'],
      'desc' => $data['desc'],
      'last_update_time' => date('Y-m-d H:i:s')
    ];
    // 新增project表
    $res = self::getModel()
      ->where('id', '=', $id)
      ->save($update);
    if ($res === false) {
      return ['status' => false, 'msg' => '编辑失败'];
    }
    return ['status' => true, 'msg' => 'ok'];
  }

  /**
   * 删除任务
   */
  public static function deleteProject($id) {
    $res = self::getModel()
      ->where('id', '=', $id)
      ->delete();
    if ($res === false) {
      return ['status' => false, 'msg' => '删除失败'];
    }
    return ['status' => true, 'msg' => 'ok'];
  }

}